﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MShop.Core.DataAccess;
using MShop.Core.Domain;
using MShop.Core.Extensions;
using MShop.Web.Components.ModelBinders;

namespace MShop.Web.Areas.Admin.Controllers
{
    public class PayController : SecureController
    {
        private readonly IPayDao _payDao;
        private readonly ICurrenyUnitDao _currenyUnitDao;
        private readonly IPayPluginDao _commonDao;
        public PayController(IPayDao payDao, ICurrenyUnitDao currenyUnitDao, IPayPluginDao commonDao)
        {
            _payDao = payDao;
            _currenyUnitDao = currenyUnitDao;
            _commonDao = commonDao;
        }
                
        public ActionResult Index()
        {
          
            return View(_payDao.GetAll());
        }

        

        public ActionResult Create(int? id)
        {
            ViewData["selectlist"] = _currenyUnitDao.GetAll().ConvertListItem("Name", "Id");
            ViewData["paytypes"] = _commonDao.GetAll().ConvertListItem("payname", "id",id);
            if (id.HasValue)
            {
                Pay pay = new Pay();
                pay.SetPayModel(_commonDao.GetPayModel(id.Value));
                return View(pay);
            }
            return View(new Pay(new CodPayModel()));
        }

        [HttpPost]
        public ActionResult Create([PayDataBind] Pay pay, FormCollection form)
        {
            
            if (ModelState.IsValid)
            {
                pay.CurrenyUnits = form["CurrenyUnits"];              
                _payDao.Save(pay);
                _payDao.SaveChanges();
                return RedirectToAction("index");
            }
            ViewData["paytypes"] = _commonDao.GetAll().ConvertListItem("payname", "id", int.Parse(pay.PayType));
            ViewData["selectlist"] = _currenyUnitDao.GetAll().ConvertListItem("Name", "Id", pay.CurrenyUnitIds);
            return View(pay);
        }

        public ActionResult Edit(int id)
        {
            var m = _payDao.GetById(id);
            ViewData["paytypes"] = _commonDao.GetAll().ConvertListItem("payname", "id", int.Parse(m.PayType), false);
            ViewData["selectlist"] = _currenyUnitDao.GetAll().ConvertListItem("Name", "Id", m.CurrenyUnitIds);
            return View(m);
        }


        public ActionResult Delete(int id)
        {
            var pay = _payDao.GetById(id);
            _payDao.Delete(pay);
            _payDao.SaveChanges();
            return RedirectToAction("index");
        }


        [HttpPost]
        public ActionResult Edit(int id, [PayDataBind] Pay pay, FormCollection form)
        { 
            if (ModelState.IsValid)
            {
                var dbpay = _payDao.GetById(id);
                dbpay.CurrenyUnits = form["CurrenyUnits"];
                UpdateModel<Pay>(dbpay, new string[] { "PayName", "HandlingCharge", "PayDesc" });
                dbpay.PayValues = pay.PayValues;
                _payDao.SaveChanges();
                return RedirectToAction("index");
            }
            return View(pay);

            
        }
    }
}
